﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Delonix_Regia_HMS.Classes;
using System.Collections;
using System.Globalization;

namespace Delonix_Regia_HMS
{
    public partial class Reporting : Form
    {
        public Reporting(string type)
        {
            InitializeComponent();
            DBManager.RetrieveRmStatus(rmStatusLview);
            DBManager.RetrieveHsekpSchedule(hsekpALview);
            if (type.Equals("Receptionist"))
            {
                reportingTabC.TabPages.Remove(managementTab);
            }
        }

        private void generateR2Btn_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(searchR2Tbx.Text))
            {
                try
                {
                    int[] guests = DBManager.RetrieveGuestsByRmNo(Convert.ToInt32(searchR2Tbx.Text));
                    if (guests[0] == 0 && guests[1] == 0)
                    {
                        adultsTbx.Text = "Room Vacant";
                        childrenTbx.Text = "or Non-Existent";
                    }
                    else
                    {
                        adultsTbx.Text = guests[0].ToString();
                        childrenTbx.Text = guests[1].ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Please input a valid Room ID!", "Error!");
                }
            }
            else
            {
                MessageBox.Show("Please input a Room ID!", "Error!");
            }
        }

        private void generateR3Btn_Click(object sender, EventArgs e)
        {
            DBManager.RetrieveGuestsAllRmsByDate(guestsAllRmLview, searchR3Dtp.Value);
        }

        private void generateR4Btn_Click(object sender, EventArgs e)
        {
            switch (searchR4Cbox.SelectedIndex)
            {
                case 0:
                    DBManager.RetrieveDailyRmOcc(rmOccLview);
                    break;

                case 1:
                    DBManager.RetrieveWeeklyRmOcc(rmOccLview);
                    break;

                case 2:
                    DBManager.RetrieveMonthlyRmOcc(rmOccLview);
                    break;

                case 3:
                    DBManager.RetrieveYearlyRmOcc(rmOccLview);
                    break;
            }
        }

        private void generateR5Btn_Click(object sender, EventArgs e)
        {
            hsekpBLview.Items.Clear();

            switch (searchR5Cbox.SelectedIndex)
            {
                case 0:
                    List<HousekeepingRecords> dailyList = DBManager.RetrieveDailyHsekp();
                    ListViewItem dailyListItem = new ListViewItem();
                    string dailyStaffDuties = "";
                    int dailyCount = 0;
                    DateTime dailyCurrDate = Convert.ToDateTime("01/01/01");
                    foreach (HousekeepingRecords h in dailyList)
                    {
                        dailyCount++;
                        if (dailyCurrDate == Convert.ToDateTime("01/01/01"))
                        {
                            dailyCurrDate = h.HousekeepingDateTime;
                            dailyListItem = new ListViewItem(dailyCurrDate.ToShortDateString());
                        }

                        if (dailyCurrDate == h.HousekeepingDateTime)
                        {
                            dailyStaffDuties += h.StaffName + " (" + h.Duties + "), ";
                        }
                        else
                        {
                            dailyListItem.SubItems.Add(dailyStaffDuties);
                            hsekpBLview.Items.Add(dailyListItem);
                            dailyCurrDate = h.HousekeepingDateTime;
                            dailyListItem = new ListViewItem(dailyCurrDate.ToShortDateString());
                            dailyStaffDuties = "";
                            dailyStaffDuties += h.StaffName + " (" + h.Duties + "), ";
                        }

                        if (dailyCount == dailyList.Count)
                        {
                            dailyListItem.SubItems.Add(dailyStaffDuties);
                            hsekpBLview.Items.Add(dailyListItem);
                        }
                    }
                    break;

                case 1:
                    List<HousekeepingRecords> weeklyList = DBManager.RetrieveWeeklyHsekp();
                    ListViewItem weeklyListItem = new ListViewItem();
                    string weeklyStaffDuties = "";
                    int weeklyCount = 0;
                    string weeklyCurrDateStart = "01/01/01";
                    string weeklyCurrDateEnd = "01/01/01";
                    foreach (HousekeepingRecords h in weeklyList)
                    {
                        weeklyCount++;
                        if (weeklyCurrDateStart.Equals("01/01/01"))
                        {
                            weeklyCurrDateStart = h.WeekStartDate;
                            weeklyCurrDateEnd = h.WeekEndDate;
                            weeklyListItem = new ListViewItem(weeklyCurrDateStart + " - " + weeklyCurrDateEnd);
                        }

                        if (weeklyCurrDateStart.Equals(h.WeekStartDate))
                        {
                            weeklyStaffDuties += h.StaffName + " (" + h.Duties + "), ";
                        }
                        else
                        {
                            weeklyListItem.SubItems.Add(weeklyStaffDuties);
                            hsekpBLview.Items.Add(weeklyListItem);
                            weeklyCurrDateStart = h.WeekStartDate;
                            weeklyCurrDateEnd = h.WeekEndDate;
                            weeklyListItem = new ListViewItem(weeklyCurrDateStart + " - " + weeklyCurrDateEnd);
                            weeklyStaffDuties = "";
                            weeklyStaffDuties += h.StaffName + " (" + h.Duties + "), ";
                        }

                        if (weeklyCount == weeklyList.Count)
                        {
                            weeklyListItem.SubItems.Add(weeklyStaffDuties);
                            hsekpBLview.Items.Add(weeklyListItem);
                        }
                    }
                    break;

                case 2:
                    List<HousekeepingRecords> monthlyList = DBManager.RetrieveDailyHsekp();
                    ListViewItem monthlyListItem = new ListViewItem();
                    string monthlyStaffDuties = "";
                    int monthlyCount = 0;
                    string monthlyCurrDate = "Month";
                    foreach (HousekeepingRecords h in monthlyList)
                    {
                        monthlyCount++;
                        if (monthlyCurrDate.Equals("Month"))
                        {
                            monthlyCurrDate = h.HousekeepingDateTime.ToString("MMMM");
                            monthlyListItem = new ListViewItem(monthlyCurrDate + " " + h.HousekeepingDateTime.Year);
                        }

                        if (monthlyCurrDate.Equals(h.HousekeepingDateTime.ToString("MMMM")))
                        {
                            monthlyStaffDuties += h.StaffName + " (" + h.Duties + "), ";
                        }
                        else
                        {
                            monthlyListItem.SubItems.Add(monthlyStaffDuties);
                            hsekpBLview.Items.Add(monthlyListItem);
                            monthlyCurrDate = h.HousekeepingDateTime.ToString("MMMM");
                            monthlyListItem = new ListViewItem(monthlyCurrDate + " " + h.HousekeepingDateTime.Year);
                            monthlyStaffDuties = "";
                            monthlyStaffDuties += h.StaffName + " (" + h.Duties + "), ";
                        }

                        if (monthlyCount == monthlyList.Count)
                        {
                            monthlyListItem.SubItems.Add(monthlyStaffDuties);
                            hsekpBLview.Items.Add(monthlyListItem);
                        }
                    }
                    break;
            }
        }

        private void refreshR1Btn_Click(object sender, EventArgs e)
        {
            DBManager.RetrieveRmStatus(rmStatusLview);
        }

        private void refreshR5Btn_Click(object sender, EventArgs e)
        {
            DBManager.RetrieveHsekpSchedule(hsekpALview);
        }
    }
}
